//
//  YWWriteIntervalTimeTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/10/11.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWWriteIntervalTimeTableViewCell.h"
#import "Masonry.h"
#import "YWMacro.h"

@interface YWWriteIntervalTimeTableViewCell()<UITextFieldDelegate>
@property (nonatomic ,strong) UITextField *noteTextField;
@property (nonatomic ,strong) UIButton *unitButton;
@end

@implementation YWWriteIntervalTimeTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setUpSubviews{
    [self.noteTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.unitButton.mas_left).offset(-10);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    [self.unitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-10);
        make.bottom.equalTo(self.contentView);
        make.width.equalTo(@(40));
    }];
}

-(void)setIntervalTime:(NSString *)intervalTime{
    _intervalTime = intervalTime;
    if (intervalTime.length) {
        self.noteTextField.text = intervalTime;
    }
}

- (void)setCurrentTimeUnit:(NSString *)currentTimeUnit{
    _currentTimeUnit = currentTimeUnit;
    [self.unitButton setTitle:currentTimeUnit forState:UIControlStateNormal];
}

- (void)setTextFieldPlaceholder:(NSString *)textFieldPlaceholder{
    _textFieldPlaceholder = textFieldPlaceholder;
    self.noteTextField.placeholder = textFieldPlaceholder;
}

- (UIButton *)unitButton{
    if (!_unitButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.titleLabel.font = kContentFont;
        [button setTitle:NSLocalizedString(@"minute", nil) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickUnitButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        _unitButton = button;
    }
    return _unitButton;
}

- (UITextField *)noteTextField{
    if (!_noteTextField) {
        UITextField * textField = [[UITextField alloc] init];
        textField.placeholder = NSLocalizedString(@"writeIntervalTime", nil);
        textField.font = kContentFont;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.delegate = self;
        [self.contentView addSubview:textField];
        _noteTextField = textField;
    }
    return _noteTextField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    //间隔时间
    NSString *intervalTime = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([self.delegate respondsToSelector:@selector(passIntervalTime:cell:)]) {
            [self.delegate passIntervalTime:intervalTime cell:self];
        }
}

- (void)clickUnitButton:(id)sender{
    //点击选择单位
    if ([self.delegate respondsToSelector:@selector(clickUnitButtonEvent:)]) {
        [self.delegate clickUnitButtonEvent:self];
    }
}

@end
